// pages/match/match.js
Page({
  data:{
    isHidden:true,
    addSure:true,//模块创建弹窗
    currentTime: '',//时间显示
    modules: [], // 存储模块的数据
    title: '', // 标题
    description: '', // 花费时间
    // 日期选择
    selectedTime: null,
    displayTime: '',
    // remainingTime: '',
    completedCount: 0, // 卡片已完成任务数
  },

  onLoad() {
    this.loadDataFromStorage();  // 页面加载时读取本地存储的数据
    //日期选择
    this.updateCountdown();
    // 时间显示
    this.updateTime();  // 加载时更新时间
    setInterval(this.updateTime, 1000); // 每秒更新一次时间
  },
  //日期选择
  onShow() {
    if (this.data.selectedTime) {
      this.updateCountdown();
    }
  },
  onUnload() {
    clearInterval(this.timer);
  },
  pop(){
    this.setData({
      isHidden:false
    })
  },
  isSure(){
    this.setData({
      isHidden:true
    })
  },
// 添加模块弹窗
  add(){
    this.setData({
      addSure:false
    })
  },
  cancel(){
    this.setData({
      addSure:true
    })
  },

updateTime() {
  const now = new Date();
  const hours = now.getHours().toString().padStart(2, '0');
  const minutes = now.getMinutes().toString().padStart(2, '0');
  const seconds = now.getSeconds().toString().padStart(2, '0');
  this.setData({
    currentTime: `${hours}:${minutes}:${seconds}`
  });
},
// 输入框内容发生变化时更新数据
onTitleChange: function (e) {
  this.setData({
    title: e.detail.value
  });
},
// 添加模块
addModule() {
  const { title, description, displayTime} = this.data;

  // 确保三个输入框都有内容
  if (title && description && displayTime) {
    const newModule = { title, description, displayTime,id: Date.now(), completed: false};
    this.setData({
      addSure:true,    
      modules: [...this.data.modules, newModule],
      title: '',
      // remainingTime:'',
      displayTime: '' ,
      description: '',
      // 清空输入框
    });
    this.updateProgress(); // 新增模块后，更新任务进度
    wx.setStorageSync('modules', this.data.modules);  // 将数据存储到本地
  } else {
    wx.showToast({
      title: '请输入完整数据',
      icon: 'none'
    });
  }
},

// 删除指定模块
deleteModule: function (e) {
  const index = e.currentTarget.dataset.index;
  const updatedModules = this.data.modules.filter((_, i) => i !== index);
  this.setData({
    modules: updatedModules
  });
  wx.setStorageSync('modules', this.data.modules);
},
// 从本地存储加载modules数据
loadDataFromStorage() {
  const storedData = wx.getStorageSync('modules');  // 获取存储的列表数据
  if (storedData) {
    this.setData({
      modules: storedData  // 更新页面数据
    });
  } else {
    console.log('本地存储没有数据');
  }
},
  // 跳转到时间选择页面
  goToTimePicker() {
    wx.navigateTo({
      url: '/pages/study/timePicker/timePicker?time=' + this.data.description,
    });
  },
  // 跳转到日期选择页面
  goToSelectTime() {
    wx.navigateTo({
      url: '/pages/study/selectDate/selectDate'
    });
  },
  updateCountdown() {
    if (!this.data.selectedTime) return;

    const interval = setInterval(() => {
      const now = new Date().getTime();
      const targetTime = new Date(this.data.selectedTime).getTime();
      const diff = targetTime - now;

      if (diff <= 0) {
        this.setData({ remainingTime: "已到时间" });
        clearInterval(interval);
      } else {
        const hours = Math.floor(diff / (1000 * 60 * 60));
        const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
        this.setData({
          remainingTime: `${hours}小时 ${minutes}分钟`,
        });
      }
    },1000);
  },
  //跳转到学习页面
goToStudy(e){
  const index=e.currentTarget.dataset.index
  wx.navigateTo({
    url: '/pages/home/focus/focus',
  });
  // console.log(this.data.modules[index])
},
  // 切换任务状态
  toggleTask(e) {
    const index = e.currentTarget.dataset.index;
    const modules = this.data.modules.map((mod, i) => {
      if (i === index) {
        return { ...mod, completed: !mod.completed }; // 切换完成状态
      }
      return mod;
    });

    this.setData({ modules }, () => {
      this.updateProgress(); // 任务状态变化后，更新进度
    });
  },
  // 计算已完成任务数
  updateProgress() {
    const completedTasks = this.data.modules.filter(mod => mod.completed).length;
    this.setData({ completedCount: completedTasks });
  }

})



